-- 自定义code actions
local null_ls = require("null-ls")

local isInStruct = function(content)
  -- vim.notify("parse struct...")
  local current_row = vim.api.nvim_win_get_cursor(0)[1]

  local start_struct = nil
  local end_struct = nil

  for i = current_row, 1, -1 do
    if content[i]:find("{") then
      if content[i]:find("%s+struct%s+{") then
        start_struct = i
        -- vim.notify(i .. content[i])
      end
      break
    end
  end

  if start_struct ~= nil then
    for i = current_row, #content do
      if content[i]:find("}") then
        end_struct = i
        -- vim.notify(i .. content[i])
        break
      end
    end
  end

  return (start_struct and end_struct and current_row >= start_struct and current_row <= end_struct)
end

local my_actions = {
  method = null_ls.methods.CODE_ACTION,
  filetypes = { "go" },
  generator = {
    fn = function(context)
      if isInStruct(context.content) then
        vim.notify("inStruct")
        return {
          {
            title = "GoTagAdd\t\tjson",
            action = function()
              vim.api.nvim_command("GoTagAdd json")
            end,
          },
          {
            title = "GoTagRm\t\tjson",
            action = function()
              vim.api.nvim_command("GoTagRm json")
            end,
          },
        }
      end
    end,
  },
}

null_ls.register(my_actions)
